﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;

namespace LabTurtorial
{

    public partial class Lab02 : Form
    {
        public Lab02()
        {
            InitializeComponent();
            cbbFontFamily.DataSource = GetAllSystemFonts();
            string currentFont = txtrContent.Font.Name;
            cbbFontFamily.SelectedItem = currentFont;
            
        }


        private void chxBold_CheckedChanged(object sender, EventArgs e)
        {
            if (chxBold.Checked)
            {
                if (chxItalic.Checked)
                {

                    txtrContent.Font = new Font(txtrContent.Font.Name, txtrContent.Font.Size, FontStyle.Bold | FontStyle.Italic);
                }
                else
                {
                    txtrContent.Font = new Font(txtrContent.Font.Name, txtrContent.Font.Size, FontStyle.Bold);
                }
            }
            else
            {
                if (chxItalic.Checked)
                {

                    txtrContent.Font = new Font(txtrContent.Font.Name, txtrContent.Font.Size, FontStyle.Italic);
                }
                else
                {
                    txtrContent.Font = new Font(txtrContent.Font.Name, txtrContent.Font.Size, FontStyle.Regular);
                }
            }
        }

        private void chxItalic_CheckedChanged(object sender, EventArgs e)
        {
            if (chxItalic.Checked)
            {
                if (chxBold.Checked)
                {
                    txtrContent.Font = new Font(txtrContent.Font.Name, txtrContent.Font.Size, txtrContent.Font.Style | FontStyle.Italic);
                }
                else
                {
                    txtrContent.Font = new Font(txtrContent.Font.Name, txtrContent.Font.Size, txtrContent.Font.Style ^ FontStyle.Italic);
                }
            }
            else
            {
                if (chxBold.Checked)
                {
                    txtrContent.Font = new Font(txtrContent.Font.Name, txtrContent.Font.Size, FontStyle.Bold);
                }
                else
                {
                    txtrContent.Font = new Font(txtrContent.Font.Name, txtrContent.Font.Size, FontStyle.Regular);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetFontFamily();
            string text = txtrContent.Font.Name + " : " + txtrContent.Font.Size + " : " + txtrContent.Font.Style.ToString();
            MessageBox.Show(text);
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            //if (rbtnArial.Checked)
            //{
            //    txtrContent.Font = new Font("Arial", txtrContent.Font.Size, txtrContent.Font.Style);

            //}
            //else {
            //    txtrContent.Font = new Font("Arial", txtrContent.Font.Size, txtrContent.Font.Style);
            //}
        }

        private List<String> GetAllSystemFonts()
        {
            List<String> fonts = new List<string>();
            foreach (FontFamily f in System.Drawing.FontFamily.Families)
            {
                fonts.Add(f.Name);
            }
            return fonts;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //int i = cbbFontFamily.SelectedIndex;
            //string values = cbbFontFamily.SelectedItem.ToString();
            //MessageBox.Show(values);
            string txt = txtrContent.SelectedText;
            MessageBox.Show(txt);

        }

        private void SetFontFamily()
        {
            string font = cbbFontFamily.SelectedItem.ToString();
            txtrContent.Font = new Font(font, txtrContent.Font.Size, txtrContent.Font.Style);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (chxUnderline.Checked)
            {
                string text = txtrContent.SelectedText;

                label1.Font = new Font(label1.Font.Name, label1.Font.Size, FontStyle.Underline);
            }
            else
            {
                label1.Font = new Font(label1.Font.Name, label1.Font.Size, FontStyle.Regular);
            }
        }
        public void SetFontStyle(object selectedRange)
        {

            //Word.Selection currSelection = null;
            //Word.Range currRange = null;

            //currSelection = selectedRange as Word.Selection;
            //if (currSelection == null)
            //    return;

            //currRange = currSelection.Range;

            //currRange.Font.Bold = 1;
            //currRange.Font.Italic = 1;

            //currRange.Select();
        }

        private void btnBold_Click(object sender, EventArgs e)
        {
            FontStyle style = txtrContent.SelectionFont.Style;
            if (txtrContent.SelectionFont.Bold)
            {
                style = style & ~FontStyle.Bold;
                btnBold.Font = new Font(btnBold.Font, FontStyle.Regular);
            }
            else
            {
                style = style | FontStyle.Bold;
                btnBold.Font = new Font(btnBold.Font, FontStyle.Bold);
            }
            txtrContent.SelectionFont = new Font(txtrContent.SelectionFont, style);
            txtrContent.Focus();
        }

        private void btnItalic_Click(object sender, EventArgs e)
        {
            FontStyle style = txtrContent.SelectionFont.Style;
            if (txtrContent.SelectionFont.Italic)
            {
                style = style & ~FontStyle.Italic;
                btnBold.Font = new Font(btnItalic.Font, FontStyle.Regular);
            }
            else
            {
                style = style | FontStyle.Italic;
                btnBold.Font = new Font(btnItalic.Font, FontStyle.Bold);
            }
            txtrContent.SelectionFont = new Font(txtrContent.SelectionFont, style);
            txtrContent.Focus();
        }

        private void btnUnderline_Click(object sender, EventArgs e)
        {
            FontStyle style = txtrContent.SelectionFont.Style;
            if (txtrContent.SelectionFont.Underline)
            {
                style = style & ~FontStyle.Underline;
                btnBold.Font = new Font(btnUnderline.Font, FontStyle.Regular);
            }
            else
            {
                style = style | FontStyle.Underline;
                btnBold.Font = new Font(btnUnderline.Font, FontStyle.Bold);
            }
            txtrContent.SelectionFont = new Font(txtrContent.SelectionFont, style);
            txtrContent.Focus();
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            txtrContent.SelectionAlignment = HorizontalAlignment.Right;
            if (btnRight.Font.Bold)
            {
                btnRight.Font = new Font(btnRight.Font, FontStyle.Regular);
            }
            else
            {
                btnRight.Font = new Font(btnRight.Font, FontStyle.Bold);
            }
            txtrContent.Focus();
        }

        private void btnCenter_Click(object sender, EventArgs e)
        {
            txtrContent.SelectionAlignment = HorizontalAlignment.Center;
            if (btnCenter.Font.Bold)
            {
                btnCenter.Font = new Font(btnCenter.Font, FontStyle.Regular);
            }
            else
            {
                btnCenter.Font = new Font(btnCenter.Font, FontStyle.Bold);
            }
            txtrContent.Focus();
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            txtrContent.SelectionAlignment = HorizontalAlignment.Left;
            if (btnLeft.Font.Bold)
            {
                btnLeft.Font = new Font(btnLeft.Font, FontStyle.Regular);
            }
            else
            {
                btnLeft.Font = new Font(btnLeft.Font, FontStyle.Bold);
            }
            txtrContent.Focus();
        }

        private void cbbFontColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbFontColor.SelectedItem.Equals("red"))
            {
                txtrContent.SelectionBackColor = Color.Red;
            }
        }

        private void cbbForeColor_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            string text = txtrContent.SelectedText;
            txtrContent.SelectionColor = Color.Yellow;

        }

        public static List<Color> ColorStructToList()
        {
            return typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public)
                                .Select(c => (Color)c.GetValue(null, null))
                                .ToList();
        }
        //private List<String> getColorName() { 
            
        //}

        private void btnTestFontColor_Click(object sender, EventArgs e)
        {
            List<Color> myList = ColorStructToList();
            Random random = new Random();
            Color color = myList[random.Next(myList.Count - 1)];
            txtrContent.BackColor = color;

        }


    }
}
